<?php
class Country {
	public function __construct() {
	
	}
	
	public function get_all_countries() {
		include("inc_/sup3rs3cr3tt3xt4m3t0kn0vv.php");
		$mysqli = new mysqli($host, $user, $password, $database);
		if(mysqli_connect_errno()) {
			printf("Connect failed: %s\n", mysqli_connect_error());
			exit();
		}
		$sql = "SELECT country_id, country_name FROM countries";
		if($result = $mysqli->query($sql)) {
			if($result->num_rows >= 1) {
				while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
					extract($row);
					$option_list[] = "<span class='listed_country'><span class='listed_country_name'>$country_name</span><span class='listed_country_id'>$country_id</span></span><br>";
				}
				return $option_list;
			}
			$result->close();
		}
		$mysqli->close();
	}
	
	public function add_country($country) {
		include("inc_/sup3rs3cr3tt3xt4m3t0kn0vv.php");
		$mysqli = new mysqli($host, $user, $password, $database);
		if(mysqli_connect_errno()) {
			printf("Connect failed: %s\n", mysqli_connect_error());
			exit();
		}
		$sql = "SELECT country_id FROM countries WHERE country_name = '$country'";
		if($result = $mysqli->query($sql)) {
			if($result->num_rows != 1) {
				$sql = "INSERT INTO countries(country_name) VALUES('$country')";
				$mysqli->query($sql);
				return 1;
			} else {
				return 0;
			}
		}
		$result->close();
	}
	
	public function check_if_exists($country) {
		include("inc_/sup3rs3cr3tt3xt4m3t0kn0vv.php");
		$mysqli = new mysqli($host, $user, $password, $database);
		if(mysqli_connect_errno()) {
			printf("Connect failed: %s\n", mysqli_connect_error());
			exit();
		}
		$sql = "SELECT country_id FROM countries WHERE country_name = '$country'";
		if($result = $mysqli->query($sql)) {
			if($result->num_rows != 1) {
				return -1;
			} else {
				while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
					extract($row);
					return $country_id;
				}
			}
		$result->close();
		}
	}
}
?>